package com.ymttest.database.redis;

import java.util.Map;
import java.util.Set;

import com.ymatou.iapi.sellercs.parameter.CsSessionDetail;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.database.RedisDriver;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.utils.EnvSetup;

import org.json.JSONException;
import redis.clients.jedis.Tuple;

public class ImRedisWapper {
	public RedisDriver rd;
	int groupid = 1;

	public ImRedisWapper() {
		rd = new RedisDriver("group" + groupid,
				EnvSetup.getSystemUrl("sentinel"));
	}

	private void setSellerId(int sellerid) {
		groupid = sellerid % 4 + 1;
		rd = new RedisDriver("group" + groupid,
				EnvSetup.getSystemUrl("sentinel"));
	}
	/*private void setSessionNumber(String sessionNumber) {
		groupid = Math.abs(sessionNumber.hashCode()) % 4;
		rd = new RedisDriver("group" + groupid,
				EnvSetup.getSystemUrl("sentinel"));
	}*/




	private void setProductId() {
		rd = new RedisDriver("group1",
				EnvSetup.getSystemUrl("sentinel"));
	}

	public Map getProductMs(String productId) {
		setProductId();
		return rd.getValueToMap("prd:pts:" + productId );
	}

	public Set<String> getLastMs(int sellerid, int limit) {
		setSellerId(sellerid);
		return rd.getLastZRange("Ms:" + sellerid, limit);

	}

	/**
	 * sessionlist
	 * @param sellerid
	 * @return
	 */
	public String getLastMs(int sellerid) {
		setSellerId(sellerid);
		return rd.getLastZRange("Ms:" + sellerid, 1).iterator().next();
	}

	/*public Map getSession(String sessionNumber){
		setSessionNumber(sessionNumber);
		return rd.getValueToMap("Msg_Buff:"+sessionNumber);
	}*/
	/**
	 * 获取消息
	 * @param sellerid
	 * @return
	 */
	public Set getMs(int sellerid) {
		setSellerId(sellerid);
		Long all=rd.getJedis().zcard("Ms:" + sellerid);
		return rd.getJedis().zrange("Ms:" + sellerid,0,all);
	}
	/**
	 * 获取IM对话
	 * @param sellerid
	 * @return
	 */
	public Set<Tuple> getIMCache(int sellerid) {
		setSellerId(sellerid);
		Set<Tuple> qs = rd.getJedis().zrangeWithScores("IMCache:0:"+sellerid,  0, -1);
		return qs;
	}
	/**
	 * 获取SellerCS队列会话列表
	 * @param sellerid
	 * @return  Set<Tuple> Element userid
	 */
	public Set<Tuple> getQSession(int sellerid) {
		setSellerId(sellerid);
		Set<Tuple> qs = rd.getJedis().zrangeWithScores("SellerCSIMCache:0:"+sellerid,  0, -1);
		return qs;
	}
	/**
	 * 获取SellerCS进行中会话列表
	 * @return  Set<Tuple> Element userid
	 */
	public Set<Tuple> getCSession(int masterSellerid) {
		setSellerId(masterSellerid);
		Logger.debug("groupid#"+groupid+"#GetCSession#"+"SellerCSIMCache:1:"+masterSellerid);
		Set<Tuple> qs = rd.getJedis().zrangeWithScores("SellerCSIMCache:1:"+masterSellerid,  0, -1);
		return qs;
	}
	/**
	 * 获取SellerCS进行中会话详情
	 * @param userid
	 * @return
	 */
	public CsSessionDetail getCSessionDetails(int masterSellerid,int userid,int csellerid) {
		setSellerId(masterSellerid);
		String ms = rd.getJedis().get("SellerCSIMCache:3:"+csellerid+":"+userid);
		CsSessionDetail csSessionDetail=(CsSessionDetail) FormatUtil.GSONToObject(ms, CsSessionDetail.class);
		return csSessionDetail;
	}
	
	/**
	 * 获取最后未读消息
	 * @param sellerid
	 * @param limit
	 * @return
	 */
	public Set<String> getLastUnMs(int sellerid, int limit) {
		setSellerId(sellerid);
		Set<String> data = rd.getLastZRange("UnMs:" + sellerid, limit);
		return data;
	}
	/**
	 * 获取未读
	 * @param sellerid
	 * @return
	 */
	public Set<String> getUnMs(int sellerid) {
		setSellerId(sellerid);
//		Long all=rd.getJedis().zcard("UnMs:" + sellerid);
		return rd.getJedis().zrange("UnMs:" + sellerid,0,-1);
//		Set<String> data = rd.getLastZRange("UnMs:" + sellerid, limit);
//		return data;
	}
	/**
	 * 未读消息
	 * @param sellerid
	 * @return
	 */
	public String getLastUnMs(int sellerid) {
		setSellerId(sellerid);
		return rd.getLastZRange("UnMs:" + sellerid, 1).iterator().next();
	}
	
	public String getCount(int sellerid,int userid) {
		setSellerId(sellerid);
		//return rd.getValue("IMCache:11:" + sellerid + ":" + "1");
		//IMCache:11:1007_3791814:1
		String count = rd.getValue("IMCache:11:"+sellerid+"_"+userid+":"+"1");
		return count;
	}
	/**
	 * 通用方法获取list 传入sellerid和key
	 * @param sellerid
	 * @param key
	 * @return
	 */
	public Set<String> getZrange(Integer sellerid,String key) {
		setSellerId(sellerid);
		return rd.getJedis().zrange(key, 0, -1);
	}

	/**
	 * 获取绑定用户信息
	 * @param key
	 * @return
	 */
	public Set<String> getBindUserRedis(String key) {
		rd = new RedisDriver("group1" ,
				EnvSetup.getSystemUrl("sentinel"));
		return rd.getJedis().zrange(key, 0, -1);
	}
	/**
	 * 获取用户绑定信息和其score
	 * @param key
	 * @return
	 */
	public Set<Tuple> getBindUserRedisWithScores(String key) {
		rd = new RedisDriver("group1" ,
				EnvSetup.getSystemUrl("sentinel"));
		return rd.getJedis().zrangeWithScores(key, 0, -1);
	}
	/**
	 * 添加/更新绑定记录
	 * @param key
	 * @param score
	 * @param member
	 */
	public void ZaddBindUserRedisWithScores(String key,Double score,String member) {
		rd = new RedisDriver("group1" ,
				EnvSetup.getSystemUrl("sentinel"));
		rd.getJedis().zadd(key, score, member);
	}
	public String getproductById(String productId)
	{
		String timeStr=rd.getValue("prd:pts:"+productId);
		System.out.println("获取同步商品redis："+timeStr);
		return timeStr;
	}

	public void updateProductTimeStamp(String productId,String timeStamp){
		rd.setValue("prd:pts:"+productId,timeStamp);
	}

	public void deleteProductTimeStamp(String productId){
		rd.getJedis().del("prd:pts:"+productId);
	}
	
	public  String get(String id)
	{
		String timeStr=rd.getValue("BindRecord:UserBindSet:2:"+id);
		System.out.println("redis guid："+timeStr);
		return timeStr;
	}
	
	public static void main(String args[]) throws JSONException {
		ImRedisWapper imw = new ImRedisWapper();
		imw.get("socketio_69454");
//		imw.getQSession(3383).forEach(q->{Logger.debug(q.getElement());});
//		// 在FromUserId%4哨兵namespace下增加key：IMCache:11:FromUserId:1
//		// 在ToUserId%4哨兵namespace下增加key：IMCache:11:ToUserId:1
//		 Logger.debug( new ImRedisWapper().getZrange(3383, "BindRecord:UserBindSet:14290223"));
//		 Logger.debug(unms);
//		// Logger.debug(last);
////		Logger.debug(imw.getCount(390572,3791815));
//		Set<String> set = imw.rd.keys("IMCache:11*");
//		for (String s : set) {
//			Logger.debug(s);
//		}
//		String productId = "p1073667";
//		new ImRedisWapper().getproductById(productId);
//		ProductManagerCallServiceV3.updateProductRedisTimeStamp(productId);
//		Logger.debug(new ImRedisWapper().getSession("24635724_24831635"));

	}
}
